package tuan.score;

import java.util.Map;
import java.util.Set;

import org.w3c.dom.Node;

import tuan.utils.RegUtil;

public class MatchTextScore extends NodeScore {
	Map<String, Double> scoreRule;

	public MatchTextScore(Map<String, Double> scoreRule) throws Exception {
		if (scoreRule == null) {
			throw new Exception("null arguments");
		}
		this.scoreRule = scoreRule;
	}

	@Override
	protected double getScore(Node node) {
		double maxScore = 0.0;
		node = node.getParentNode();
		Set<String> patternSet = scoreRule.keySet();
		for (String pattern : patternSet) {
			if (RegUtil.match(pattern, node.getTextContent())) {
				double score = scoreRule.get(pattern);
				maxScore = maxScore < score ? score : maxScore;
			}
		}
		return maxScore;
	}

}
